page.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. "use client";
  2. import type {ProColumns} from "@ant-design/pro-components";
  3. import {PageContainer, ProCard, ProDescriptions, ProSkeleton, ProTable,} from "@ant-design/pro-components";
  4. import {Button, Divider, Flex} from "antd";
  5. import {fetchApi} from "@/app/_modules/func";
  6. import {useRouter} from "next/navigation";
  7. import {useEffect, useState} from "react";
  8. import globalMessage from "@/app/_modules/globalMessage";
  9. export default function UserAuth({ params }: { params: { userid: string } }) {
  10. const { push } = useRouter();
  11. //用户信息
  12. const [user, setUser] = useState<any>({});
  13. //角色信息
  14. const [roles, setRoles] = useState([]);
  15. const getUserData = async () => {
  16. const body = await fetchApi(
  17. `/api/system/user/authRole/${params.userid}`,
  18. push
  19. );
  20. if (body !== undefined) {
  21. if (body.code == 200) {
  22. setUser(body.user);
  23. setRoles(body.roles);
  24. setSelectedRowKeys(body.user.roles.map((item: any) => item.roleId));
  25. }
  26. }
  27. };
  28. useEffect(() => {
  29. getUserData();
  30. }, []);
  31. //表格列定义
  32. const columns: ProColumns[] = [
  33. {
  34. title: "序号",
  35. dataIndex: "index",
  36. valueType: "indexBorder",
  37. width: 48,
  38. },
  39. {
  40. title: "角色编号",
  41. dataIndex: "roleId",
  42. search: false,
  43. },
  44. {
  45. title: "角色名称",
  46. dataIndex: "roleName",
  47. search: false,
  48. },
  49. {
  50. title: "权限字符",
  51. dataIndex: "roleKey",
  52. search: false,
  53. },
  54. {
  55. title: "创建时间",
  56. dataIndex: "createTime",
  57. },
  58. ];
  59. //选中行操作
  60. const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]);
  61. const rowSelection = {
  62. onChange: (newSelectedRowKeys: React.Key[]) => {
  63. setSelectedRowKeys(newSelectedRowKeys);
  64. },
  65. };
  66. //当前每页条数
  67. const defaultPageSize = 10;
  68. //提交按钮加载状态
  69. const [confirmLoding, setConfirmLoading] = useState(false);
  70. //更新用户角色
  71. const updateAuth = async () => {
  72. setConfirmLoading(true);
  73. const queryParam = {
  74. userId: user.userId,
  75. roleIds: selectedRowKeys.join(","),
  76. };
  77. const body = await fetchApi(
  78. `/api/system/user/authRole?${new URLSearchParams(queryParam)}`,
  79. push,
  80. {
  81. method: "PUT",
  82. }
  83. );
  84. if (body !== undefined) {
  85. if (body.code == 200) {
  86. globalMessage.success("授权成功");
  87. } else {
  88. globalMessage.error(body.msg);
  89. }
  90. }
  91. setConfirmLoading(false);
  92. };
  93. return (
  94. <PageContainer
  95. header={{
  96. title: "分配角色",
  97. onBack(e) {
  98. push("/system/user");
  99. },
  100. }}
  101. >
  102. {Object.keys(user).length === 0 ? (
  103. <ProSkeleton type="list" />
  104. ) : (
  105. <>
  106. <ProCard title="基本信息">
  107. <ProDescriptions column={24}>
  108. <ProDescriptions.Item span={12} label="用户昵称">
  109. {user.nickName}
  110. </ProDescriptions.Item>
  111. <ProDescriptions.Item span={12} label="用户名称">
  112. {user.userName}
  113. </ProDescriptions.Item>
  114. </ProDescriptions>
  115. </ProCard>
  116. <Divider />
  117. <ProCard title="角色信息">
  118. <ProTable
  119. rowKey="roleId"
  120. rowSelection={{
  121. selectedRowKeys,
  122. ...rowSelection,
  123. }}
  124. tableAlertRender={false}
  125. columns={columns}
  126. dataSource={roles}
  127. pagination={{
  128. defaultPageSize: defaultPageSize,
  129. showQuickJumper: true,
  130. showSizeChanger: true,
  131. // onChange: pageChange,
  132. }}
  133. search={false}
  134. dateFormatter="string"
  135. toolbar={{
  136. actions: [],
  137. settings: [],
  138. }}
  139. />
  140. </ProCard>
  141. <ProCard>
  142. <Flex justify="center" gap="middle">
  143. <Button href="/system/user">返回</Button>
  144. <Button
  145. type="primary"
  146. onClick={updateAuth}
  147. loading={confirmLoding}
  148. >
  149. 提交
  150. </Button>
  151. </Flex>
  152. </ProCard>
  153. </>
  154. )}
  155. </PageContainer>
  156. );
  157. }